package ua.org.nuos.sdms.clientgui.client.handlers;

import ua.org.nuos.sdms.clientgui.client.context.Messages;
import ua.org.nuos.sdms.clientgui.client.controller.GlobalBoard;
import ua.org.nuos.sdms.clientgui.client.events.ErrorMessageEvent;
import ua.org.nuos.sdms.clientgui.client.events.RefuseFromLeaderTypeEvent;
import ua.org.nuos.sdms.clientgui.client.events.ShowDialogMessageEvent;
import ua.org.nuos.sdms.clientgui.client.listeners.DialogResultHandler;
import ua.org.nuos.sdms.clientgui.client.listeners.RefuseFromLeaderTypeListener;
import ua.org.nuos.sdms.clientgui.server.AppProxy;
import ua.org.nuos.sdms.middle.entity.Group;
import ua.org.nuos.sdms.middle.util.exception.OneGroupLeaderException;
import ua.org.nuos.sdms.middle.util.exception.UpdateEntityException;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Created by IntelliJ IDEA.
 * User: dio
 * Date: 08.04.12
 * Time: 20:44
 * To change this template use File | Settings | File Templates.
 */
public class RefuseFromLeaderTypeHandler implements RefuseFromLeaderTypeListener {
    @Override
    public void refuse(RefuseFromLeaderTypeEvent event) {
        ShowDialogMessageEvent e = new ShowDialogMessageEvent(getDialogResultHandler(), "Внимание!", "<b>Отказаться от лидерства?</b>");
        AppProxy.getInstance().getController(GlobalBoard.ID).fire(e);
    }

    private DialogResultHandler getDialogResultHandler() {
        return new DialogResultHandler() {
            @Override
            public void dialogResultProcess(boolean accepted) {
                if (accepted) {
                    Group group = AppProxy.getInstance().getModelLocator().getGroup();
                    try {
                        AppProxy.getInstance().getServiceLocator().getClientBean().updateCurrentUserGroupTypeFromLeaderToUser(group.getId());
                        AppProxy.getInstance().getModelLocator().getGroupContainer().fireListeners();
                    } catch (OneGroupLeaderException e) {
                        Logger.getAnonymousLogger().log(Level.WARNING, "Update curent user type in group to \"USER\" error", e);
                        AppProxy.getInstance().getControllerLocator().getController(GlobalBoard.ID).fire(
                                new ErrorMessageEvent(String.format(Messages.REFUSE_FROM_GROUP_LEADER_TYPE_ONE_LEADER_ERROR, group.getName())));
                    } catch (UpdateEntityException e) {
                        Logger.getAnonymousLogger().log(Level.WARNING, "Update curent user type in group to \"USER\" error", e);
                        AppProxy.getInstance().getControllerLocator().getController(GlobalBoard.ID).fire(new ErrorMessageEvent(Messages.ACTION_ERROR));
                    } catch (Exception e) {
                        Logger.getAnonymousLogger().log(Level.WARNING, "Update curent user type in group to \"USER\" error", e);
                        AppProxy.getInstance().getControllerLocator().getController(GlobalBoard.ID).fire(new ErrorMessageEvent(Messages.ACTION_ERROR));
                    }
                }
            }
        };
    }
}
